#! /usr/bin/python
# -*- coding: utf-8 -*-

import os
import glob
import time

import md5
import sha
import hashlib

# todos:
## should test on gnulinux
## should include doctests
## consider using class

def chs_md5(fpath):
    """
    takes a file path and returns a hash of its string
    """
    fp=open(fpath,'rb')
    s = md5.new()
    rB = 4096 # read 4096 bytes at per cycle
    while(rB):
        data = fp.read(rB)
        s.update(data)
        rB = len(data)
    fp.close()
    return s.hexdigest()

def chs_sha1(fpath):
    """
    takes a file path and returns a SHA hash of its string
    inspired/credit by/to: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/259109
    """
    fp=open(fpath,'rb')
    s = sha.new()
    rB = 4096 # read 4096 bytes at per cycle
    while(rB):
        data = fp.read(rB)
        s.update(data)
        rB = len(data)
    fp.close()
    return s.hexdigest()

def chs_sha256(fpath):
    """
    takes a file path and returns a SHA hash of its string
    """
    fp=open(fpath,'rb')
    s = hashlib.new('sha256')
    rB = 4096 # read 4096 bytes at per cycle
    while(rB):
        data = fp.read(rB)
        s.update(data)
        rB = len(data)
    fp.close()
    return s.hexdigest()

if __name__ == '__main__':
    ''' '''
    print chs_md5('checksums.py')
    print chs_sha1('checksums.py')
    print chs_sha256('checksums.py')

    """
        
     : md5 :
    9bf132c4fc027b3e1043d4d686d812e3 *ofwboot.net
    ; Generated by Altap Salamander, http://www.altap.cz
    9bf132c4fc027b3e1043d4d686d812e3 ofwboot.net
    
     : sha1 :
    0adddde317ec4486c5114aa9162e2f8957698b7d *ofwboot.net
    sha1sum GNULinux utility
    0adddde317ec4486c5114aa9162e2f8957698b7d *ofwboot.net
    
     : sha256 :
    4d01b1ad70c9ca56024644fb08373eea5be3829412a1d8becbc611ebf517fe9c *ofwboot.net
    http://ftp.nluug.nl/OpenBSD/snapshots/sparc64/SHA256
    SHA256 (ofwboot.net) = 4d01b1ad70c9ca56024644fb08373eea5be3829412a1d8becbc611ebf517fe9c
    sha256sum GNULinux utility
    4d01b1ad70c9ca56024644fb08373eea5be3829412a1d8becbc611ebf517fe9c *ofwboot.net
    
     Generated by : pepe.p_utils.chs.py v1.2
     
    """
    